<?php
function hw(){
	return "Hello, World!";
}

function ololo(){
	return "Ololo? Ololo!";
}

function joke(){
	return "I have no joke...";
}
   $address = 'localhost';
    $port    = 10001;
	if (FALSE == ($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP))) {
		die("Couldn't work with socket, error code is: " . socket_last_error() .",error message is: " . socket_strerror(socket_last_error()));
    }
    if (FALSE == ($ret = socket_bind($sock, $address, $port))) {
        die("Couldn't work with socket, error code is: " . socket_last_error() .",error message is: " . socket_strerror(socket_last_error()));
    }
	if (FALSE == ($ret = socket_listen($sock, 1))) {
		die("Couldn't work with socket, error code is: " . socket_last_error() .",error message is: " . socket_strerror(socket_last_error()));
    }
	do {
		if (FALSE == ($msgsock = socket_accept($sock))) {
			die("Couldn't work with socket, error code is: " . socket_last_error() .",error message is: " . socket_strerror(socket_last_error()));
		}
		else{
			if (FALSE == ($buf = socket_read($msgsock, 1024))){
				die ('socket_read() failed: '.socket_strerror(socket_last_error()));
			}
				$t = function_exists($buf); 
				if ($t == TRUE) {
					$res = $buf();
				} else{
					$res =  "Sorry, i don't know this function";
				}
				socket_write($msgsock, $res);
		}
	} while (true);